package gameserver;

import java.io.*;
import java.net.HttpURLConnection;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;


import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.Headers;

public class GameHandlerStatic implements HttpHandler {

	private String filePath;
	
	public GameHandlerStatic(String filePath) {
		this.filePath = filePath;
	}
	
	private static String readFile(String path) throws IOException {
		  FileInputStream stream = new FileInputStream(new File(path));
		  try {
		    FileChannel fc = stream.getChannel();
		    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
		    /* Instead of using default, pass in a decoder. */
		    return Charset.defaultCharset().decode(bb).toString();
		  }
		  finally {
		    stream.close();
		  }
	}
	
	public void handle(HttpExchange exchange) throws IOException {
		
		String response = this.readFile(this.filePath);
		InputStream input = exchange.getRequestBody();
		OutputStream output = exchange.getResponseBody();
		
		exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length());
        exchange.getResponseBody().write(response.getBytes());
        exchange.close();
		
	}
}


